package com.jt.controll;

import com.jt.pojo.Employee;
import com.jt.pojo.User;
import com.jt.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@ResponseBody
@CrossOrigin
@RequestMapping("/user")
public class UserControll {

    @Autowired
    private UserServiceImpl userService;

    //获取所有的用户的列表
    @RequestMapping("/list")
    public  List<User>  list(){
        System.out.println("获取用户列表的请求");
        List<User> list = userService.list();
        return   list;
    }

    //根据id获取单个用户信息
    @GetMapping("/get/{id}")
    public  User get(@PathVariable("id") Integer  id){
        System.out.println("获取单个用户的请求");
        User one = userService.getOne(id);
        return one;
    }

    //根据id获取单个用户信息
    @GetMapping("/get")
    public  User getById(@RequestBody User user){
        System.out.println(user);
      ///  System.out.println("获取单个用户的请求"+id);
        User one = userService.getOne(user.getId());
        return one;
    }


    /**
     *
     * @url:/user/addUser
     * @method:post
     * @参数:form表单传参  name/age/sex
     * 返回值,返回成功的参数
     * @param user
     * @return
     */
    @PostMapping("/add")
    public String addUser(@RequestBody User user){
        System.out.println("post请求");
        System.out.println(user);
        boolean save = userService.save(user);
        if(save)
            return "新增用户成功";
        else
            return  "新增用户失败";
    }

    //根据id删除用户的信息
    @DeleteMapping("/delete/{id}")
    public   String   deleteUser(@PathVariable("id") Integer  id){
        System.out.println("删除请求");
        userService.removeById(id);
        //List<User> list = userService.list();
        return   "删除成功";
    }

    @PutMapping("/update")
    public   User   updateUser(@RequestBody User user){
        System.out.println(user);
        userService.updateUser(user);
        return   null;
    }

}
